package com.cb.controller.admin.system;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import com.cb.core.command.QueryFilter;
import com.cb.core.command.QueryResult;
import com.cb.core.controller.BaseListController;

import com.cb.domain.model.system.Company;
import com.cb.domain.service.system.CompanyManager;

/**
 * 
 * @Author author
 * @Vesion 1.0
 * Create Time:
 * <p><b>Class Description:</b>[TODO]<p>
 */
public class CompanyController extends BaseListController {
	
	@Autowired
	private CompanyManager companyManager;

	
	/**对象列表页，提供最基本的分页显示
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		QueryResult companys=companyManager.getAll(new QueryFilter(request));
		
		return new ModelAndView("admin/system/companys.do?cmd=viewDetail","companys",companys);
	}
	
	/**
	 * 批量删除，通过页面传回对象的主键数组进行逐条删除
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView multiDelete(HttpServletRequest request,	HttpServletResponse response) {
		String[] companyIds=request.getParameterValues("companyId");
		if(companyIds!=null){
			for(String companyId:companyIds){
				companyManager.remove(new Long(companyId));
			}
			saveMessage(request, getText("company.mutiDeleted"));
		}
		String url=request.getHeader("Referer");
		if(StringUtils.isNotEmpty(url)){
			return new ModelAndView("redirect:"+url);
		}
		return new ModelAndView(getSuccessView());
	}
	
	/**
	 * 删除单个实体对象
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView delete(HttpServletRequest request,	HttpServletResponse response){
		String companyId=request.getParameter("companyId");
		if(StringUtils.isNotEmpty(companyId)){
			Company company=companyManager.get(new Long(companyId));
			companyManager.remove(company);
			saveMessage(request,getText("company.deleted",new Object[]{company.getCompanyId()}));
		}
		String url=request.getHeader("Referer");
		if(StringUtils.isNotEmpty(url)){
			return new ModelAndView("redirect:"+url);
		}
		return new ModelAndView(getSuccessView());
	}
	
	/**
	 * 查看实体对象详细信息
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView viewDetail(HttpServletRequest request,	HttpServletResponse response){
		String companyId=request.getParameter("companyId");
		if(companyId==null || companyId.equals("")){
			companyId = "1";
		}
		Company company=null;
		if(StringUtils.isNotEmpty(companyId)){
			company=companyManager.get(new Long(companyId));
		}
		return new ModelAndView("admin/system/companyDetail","company",company);
	}
}
